<?php

class categoryPageActions extends myFrontendAction
{
	public function executeIndex(sfWebRequest $request)
    {
        $limit   = intval($request->getParameter('limit'));
        $offset  = 0;
        
        // Calculate offset
        if($request->hasParameter('limit') && $request->hasParameter('page'))
        {
            $offset = (($request->getParameter('page') - 1) * $request->getParameter('limit'));
        }
        
        $productModel   = new Product;
        $this->products = $productModel->getLastCategoryProducts(
            $this->getUser(),
            $this->getRoute()->getCategoryObject()->getId(),
            $limit,
            $offset
        );
        $this->links   = LinkGen::getInstance(LinkGen::PRODUCT)->collectionLinks($this->products);
        $this->count   = $productModel->getLastCategoryProductsCount($this->getUser(), $this->getRoute()->getCategoryObject()->getId());
        $this->current = $request->hasParameter('page') ? $request->getParameter('page') : 1;
        $this->pages   = 0;
        
        if($limit > 0)
        {
            $this->pages = round($this->count / $limit);
        }
	}
}